package com.tang.thepath;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class LiveService extends Service {

    public class LiveBinder extends Binder{

        public LiveService getService(){
            return LiveService.this;
        }
    }

    //通过binder实现了 调用者（client）与 service之间的通信
    private LiveBinder binder = new LiveBinder();

    public LiveService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        Thread t1 =  new Thread(new Runnable() {
//
//            @Override
//            public void run() {
//                useForeground("正在运行","正在运行");
//                while (true){
//                    Log.e("LiveService","LiveService is running");
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//        t1.start();
        Log.e("LiveService","LiveService is onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }
    public void useForeground(CharSequence tickerText, String currSong) {
        Intent notificationIntent = new Intent(getApplicationContext(), LiveActivity.class);
        notificationIntent.putExtra("auto_finish",true);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
        /* Method 01
         * this method must SET SMALLICON!
         * otherwise it can't do what we want in Android 4.4 KitKat,
         * it can only show the application info page which contains the 'Force Close' button.*/
        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this)
                .setPriority(Notification.PRIORITY_MAX)
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker(tickerText)
                .setWhen(System.currentTimeMillis())
                .setContentTitle(getString(R.string.app_name))
                .setContentText(currSong)
                .setContentIntent(pendingIntent);
        Notification notification = mNotifyBuilder.build();

        /* Method 02
        Notification notification = new Notification(R.drawable.ic_launcher, tickerText,
                System.currentTimeMillis());
        notification.setLatestEventInfo(PlayService.this, getText(R.string.app_name),
                currSong, pendingIntent);
        */

        startForeground(99911, notification);
    }
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("LiveService","LiveService is onBind");
        useForeground("正在运行","正在运行");
        return binder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("LiveService","LiveService is onUnbind");
        stopForeground(true);
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.e("LiveService","LiveService is onDestroy");
        stopForeground(true);
        super.onDestroy();
    }
}
